using System.Xml;
using ZoneFiveSoftware.Common.Visuals.Fitness;

using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

namespace FTPPlugin
{
    class Plugin : IPlugin
    {
        #region IPlugin Members

        public IApplication Application
        {
            set { application = value; }
        }

        public Guid Id
        {
            get { return new Guid("{85625972-f115-4c01-ab15-f3359db445c0}"); }
        }

        public string Name
        {
            get { return "FTP Upload Plugin"; }
        }

        public void ReadOptions(XmlDocument xmlDoc, XmlNamespaceManager nsmgr, XmlElement pluginNode)
        {
            FTPUserUrl = pluginNode.GetAttribute(xmlTags.FTPUrl);
            FTPUserFile = pluginNode.GetAttribute(xmlTags.FTPFile);
            FTPUserName = pluginNode.GetAttribute(xmlTags.FTPUser);
            FTPUserPassword = pluginNode.GetAttribute(xmlTags.FTPPassword);
            String attr = pluginNode.GetAttribute(xmlTags.FTPSavePassword);
            if (attr.Length > 0)
                FTPUserSavePassword = XmlConvert.ToBoolean(attr);
            FTPUserAPIKey = pluginNode.GetAttribute(xmlTags.FTPAPIKey);
            FTPUserPageTitle = pluginNode.GetAttribute(xmlTags.FTPPageTitle);
            attr = pluginNode.GetAttribute(xmlTags.FTPLocation);
            if (attr.Length > 0)
                FTPUserLocation = XmlConvert.ToBoolean(attr);
            attr = pluginNode.GetAttribute(xmlTags.FTPStartTime);
            if (attr.Length > 0)
                FTPUserStartTime = XmlConvert.ToBoolean(attr);
            attr = pluginNode.GetAttribute(xmlTags.FTPCategory);
            if (attr.Length > 0)
                FTPUserCategory = XmlConvert.ToBoolean(attr);
            attr = pluginNode.GetAttribute(xmlTags.FTPDistance);
            if (attr.Length > 0)
                FTPUserDistance = XmlConvert.ToBoolean(attr);
            attr = pluginNode.GetAttribute(xmlTags.FTPTotalTime);
            if (attr.Length > 0)
                FTPUserTotalTime = XmlConvert.ToBoolean(attr);
            attr = pluginNode.GetAttribute(xmlTags.FTPAvgSpeed);
            if (attr.Length > 0)
                FTPUserAvgSpeed = XmlConvert.ToBoolean(attr);
            attr = pluginNode.GetAttribute(xmlTags.FTPCalories);
            if (attr.Length > 0)
                FTPUserCalories = XmlConvert.ToBoolean(attr);
            attr = pluginNode.GetAttribute(xmlTags.FTPWeather);
            if (attr.Length > 0)
                FTPUserWeather = XmlConvert.ToBoolean(attr);
            attr = pluginNode.GetAttribute(xmlTags.FTPEquipment);
            if (attr.Length > 0)
                FTPUserEquipment = XmlConvert.ToBoolean(attr);
            attr = pluginNode.GetAttribute(xmlTags.FTPNotes);
            if (attr.Length > 0)
                FTPUserNotes = XmlConvert.ToBoolean(attr);
            attr = pluginNode.GetAttribute(xmlTags.FTPMap);
            if (attr.Length > 0)
                FTPUserMap = XmlConvert.ToBoolean(attr);
            FTPUserTemplate = pluginNode.GetAttribute(xmlTags.FTPTemplate);
            attr = pluginNode.GetAttribute(xmlTags.FTPFileFTP);
            if (attr.Length > 0)
                FTPUserFTPFile = XmlConvert.ToBoolean(attr);
            FTPUserMapType = pluginNode.GetAttribute(xmlTags.FTPMapType);
            FTPUserDateFormat = pluginNode.GetAttribute(xmlTags.FTPDateFormat);
            attr = pluginNode.GetAttribute(xmlTags.FTPSuccessMessage);
            if (attr.Length > 0)
                FTPUserSuccessMessage = XmlConvert.ToBoolean(attr);
            FTPUserIndexFile = pluginNode.GetAttribute(xmlTags.FTPIndexFile);
            attr = pluginNode.GetAttribute(xmlTags.FTPUpdateIndex);
            if (attr.Length > 0)
                FTPUserUpdateIndex = XmlConvert.ToBoolean(attr);
            attr = pluginNode.GetAttribute(xmlTags.FTPGraphSplits);
            if (attr.Length > 0)
                FTPUserGraphSplits = XmlConvert.ToBoolean(attr);
            attr = pluginNode.GetAttribute(xmlTags.FTPUploadJava);
            if (attr.Length > 0)
                FTPUserUploadJava = XmlConvert.ToBoolean(attr);
            attr = pluginNode.GetAttribute(xmlTags.FTPSplitDistance); 
            if (attr.Length > 0) 
                FTPUserSplitDistance = pluginNode.GetAttribute(xmlTags.FTPSplitDistance);
            attr = pluginNode.GetAttribute(xmlTags.FTPPassive);
            if (attr.Length > 0)
                FTPUserPassive = XmlConvert.ToBoolean(attr);
        }

        public string Version
        {
            get { return GetType().Assembly.GetName().Version.ToString(3); }
        }

        public void WriteOptions(XmlDocument xmlDoc, XmlElement pluginNode)
        {
            pluginNode.SetAttribute(xmlTags.FTPUrl, FTPUserUrl);
            pluginNode.SetAttribute(xmlTags.FTPFile, FTPUserFile);
            pluginNode.SetAttribute(xmlTags.FTPUser, FTPUserName);
            pluginNode.SetAttribute(xmlTags.FTPPassword, FTPUserPassword);
            pluginNode.SetAttribute(xmlTags.FTPSavePassword, XmlConvert.ToString(FTPUserSavePassword));
            pluginNode.SetAttribute(xmlTags.FTPAPIKey, FTPUserAPIKey);
            pluginNode.SetAttribute(xmlTags.FTPPageTitle, FTPUserPageTitle);
            pluginNode.SetAttribute(xmlTags.FTPLocation, XmlConvert.ToString(FTPUserLocation));
            pluginNode.SetAttribute(xmlTags.FTPStartTime, XmlConvert.ToString(FTPUserStartTime));
            pluginNode.SetAttribute(xmlTags.FTPCategory, XmlConvert.ToString(FTPUserCategory));
            pluginNode.SetAttribute(xmlTags.FTPDistance, XmlConvert.ToString(FTPUserDistance));
            pluginNode.SetAttribute(xmlTags.FTPTotalTime, XmlConvert.ToString(FTPUserTotalTime));
            pluginNode.SetAttribute(xmlTags.FTPAvgSpeed, XmlConvert.ToString(FTPUserAvgSpeed));
            pluginNode.SetAttribute(xmlTags.FTPCalories, XmlConvert.ToString(FTPUserCalories));
            pluginNode.SetAttribute(xmlTags.FTPWeather, XmlConvert.ToString(FTPUserWeather));
            pluginNode.SetAttribute(xmlTags.FTPEquipment, XmlConvert.ToString(FTPUserEquipment));
            pluginNode.SetAttribute(xmlTags.FTPNotes, XmlConvert.ToString(FTPUserNotes));
            pluginNode.SetAttribute(xmlTags.FTPMap, XmlConvert.ToString(FTPUserMap));
            pluginNode.SetAttribute(xmlTags.FTPTemplate, FTPUserTemplate);
            pluginNode.SetAttribute(xmlTags.FTPFileFTP, XmlConvert.ToString(FTPUserFTPFile));
            pluginNode.SetAttribute(xmlTags.FTPMapType, FTPUserMapType);
            pluginNode.SetAttribute(xmlTags.FTPDateFormat, FTPUserDateFormat);
            pluginNode.SetAttribute(xmlTags.FTPSuccessMessage, XmlConvert.ToString(FTPUserSuccessMessage));
            pluginNode.SetAttribute(xmlTags.FTPIndexFile, FTPUserIndexFile);
            pluginNode.SetAttribute(xmlTags.FTPUpdateIndex, XmlConvert.ToString(FTPUserUpdateIndex));
            pluginNode.SetAttribute(xmlTags.FTPGraphSplits, XmlConvert.ToString(FTPUserGraphSplits));
            pluginNode.SetAttribute(xmlTags.FTPUploadJava, XmlConvert.ToString(FTPUserUploadJava));
            pluginNode.SetAttribute(xmlTags.FTPSplitDistance, FTPUserSplitDistance);
            pluginNode.SetAttribute(xmlTags.FTPPassive, XmlConvert.ToString(FTPUserPassive));
        }

        #endregion
        public static IApplication GetApplication()
        {
            return application;
        }

        #region Private members
        private static IApplication application;
        #endregion
        private class xmlTags
        {
            public const string FTPUrl = "FTPUrl";
            public const string FTPFile = "FTPFile";
            public const string FTPUser = "FTPUser";
            public const string FTPPassword = "FTPPassword";
            public const string FTPAPIKey = "FTPAPIKey";
            public const string FTPSavePassword = "FTPSavePassword";
            public const string FTPPageTitle = "FTPPageTitle";
            public const string FTPLocation = "FTPLocation";
            public const string FTPStartTime = "FTPStartTime";
            public const string FTPCategory = "FTPCategory";
            public const string FTPDistance = "FTPDistance";
            public const string FTPTotalTime = "FTPTotalTime";
            public const string FTPAvgSpeed = "FTPAvgSpeed";
            public const string FTPCalories = "FTPCalories";
            public const string FTPWeather = "FTPWeather";
            public const string FTPEquipment = "FTPEquipment";
            public const string FTPNotes = "FTPNotes";
            public const string FTPMap = "FTPMap";
            public const string FTPTemplate = "FTPTemplate";
            public const string FTPFileFTP = "FTPFileFTP";
            public const string FTPMapType = "FTPMapType";
            public const string FTPDateFormat = "FTPDateFormat";
            public const string FTPSuccessMessage = "FTPSuccessMessage";
            public const string FTPIndexFile = "FTPIndexFile";
            public const string FTPUpdateIndex = "FTPUpdateIndex";
            public const string FTPGraphSplits = "FTPGraphSplits";
            public const string FTPUploadJava = "FTPUploadJava";
            public const string FTPSplitDistance = "FTPSplitDistance";
            public const string FTPPassive = "FTPPassive";
        }
/* Values loaded here are default and will be overwritten once settings are set the first time */        
        public static string FTPUserUrl;
        public static string FTPUserFile;
        public static string FTPUserName;
        public static string FTPUserPassword;
        public static string FTPUserAPIKey;
        public static bool FTPUserSavePassword;
        public static string FTPUserPageTitle = "Sport Tracks Activity Report";
        public static bool FTPUserLocation = true;
        public static bool FTPUserStartTime = true;
        public static bool FTPUserCategory = true;
        public static bool FTPUserDistance = true;
        public static bool FTPUserTotalTime = true;
        public static bool FTPUserAvgSpeed = true;
        public static bool FTPUserCalories = true;
        public static bool FTPUserWeather = true;
        public static bool FTPUserEquipment = true;
        public static bool FTPUserNotes = true;
        public static bool FTPUserMap = true;
        public static string FTPUserTemplate = "Generate New";
        public static bool FTPUserFTPFile = true;
        public static string FTPUserMapType;
        public static string FTPUserDateFormat;
        public static bool FTPUserSuccessMessage = true;
        public static string FTPUserIndexFile;
        public static bool FTPUserUpdateIndex = true;
        public static bool FTPUserGraphSplits = true;
        public static bool FTPUserUploadJava = true;
        public static string FTPUserSplitDistance = "1";
        public static bool FTPUserPassive = true;

        public static string directoryString = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + Path.DirectorySeparatorChar + "ZoneFiveSoftware" + Path.DirectorySeparatorChar + "SportTracks" + Path.DirectorySeparatorChar + "2.0" + Path.DirectorySeparatorChar + "Plugins" + Path.DirectorySeparatorChar + "FTPPlugin" + Path.DirectorySeparatorChar;
    }
}
